package com.googlecode.dtools.typeinfo.graphviz;

import com.googlecode.dtools.typeinfo.graphviz.worker.GraphvizWorker;
import com.googlecode.dtools.typeinfo.util.SpringUtils;
import com.googlecode.dtools.typeinfo.work.FilesWorker;

import java.io.File;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author A_Reshetnikov
 * @since Date: 25.05.12 Time: 10:47
 */
public class GraphvizMain {

    private static String outputDir;
    private static boolean justPrintUsage = false;

    public static void main(String[] args) throws Exception {
        collectArgs(args);
        List<String> list;
               // = (List<String>) SpringUtils.getSpringBean("typesList");
        FilesWorker filesWorker = (FilesWorker) SpringUtils.getSpringBean("filesWorker");
        if (outputDir != null) {
            modifyWorkerConfig(filesWorker);
        }

        list = filesWorker.readTypeNameListFromFileSystem();
        System.out.println("list.size() = " + list.size());
        GraphvizWorker graphvizWorker = (GraphvizWorker) SpringUtils.getSpringBean("graphvizWorker");
        graphvizWorker.addListToHolder(list);
        graphvizWorker.workList(list);

    }

    private static void modifyWorkerConfig(FilesWorker filesWorker) {
        filesWorker.setOutputPath(outputDir);
    }


    private static void collectArgs(String[] args) throws Exception {

        for (int i = 0; i < args.length; i++) {
            String arg = args[i];
            if (arg.equals("-help") || arg.equals("-h")) {
                justPrintUsage = true;
            } else if (arg.equals("-o") || arg.equals("-output")) {
                outputDir = args[i + 1];
                i++;
                createDir();
            } else if (arg.startsWith("-")) {
                // we don't have any more args to recognize!
                String msg = "Unknown argument: " + arg;
                System.err.println(msg);
                printUsage();
                throw new Exception("");
            }
        }
    }

    private static void createDir() throws Exception {
        if (!outputDir.endsWith("\\")) {
            outputDir += "\\";
        }
        try {
            File dir = new File(outputDir);
            if (!dir.exists()) {
                // Create directory
                boolean success = dir.mkdir();
                if (success) {
                    System.out.println("Directory: "
                            + outputDir + " created");
                }
            }
        } catch (Exception e) {
            System.err.println("Cannot create output directory with name, exit");
            throw new Exception("");
        }
    }


    /**
     * Prints the usage information for this class to <code>System.out</code>.
     */
    private static void printUsage() {
        String lSep = System.getProperty("line.separator");
        StringBuffer msg = new StringBuffer();
        msg.append("Options: " + lSep);
        msg.append("  -help, -h              print this message" + lSep);
        msg.append("  -output, -o            output dir" + lSep);
        System.out.println(msg.toString());
    }

}
